﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Sern.Common
{
    public static class ObjectExtension
    {
        /// <summary>
        /// Allows deepcloning operation.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="object"></param>
        /// <returns></returns>
        public static T DeepClone<T>(this T @object)
        {
            var serializer = new NetDataContractSerializer();
            using (var stream = new MemoryStream())
            {
                serializer.WriteObject(stream,@object);
                stream.Position = 0; //Resets position.
                return (T) serializer.ReadObject(stream);
            }
        }
    }
}
